// 2025/11/21
// 搜索旋转排序数组

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int n = nums.size();
        int l = 0, r = n - 1, begin = 0;
        while(l <= r)
        {
            int mid = (l + r) >> 1;
            if(nums[mid] <= nums[n - 1])
            {
                begin = mid;
                r = mid - 1;
            }
            else
                l = mid + 1;
        }
        if(begin == 0)
        {
            l = 0, r = n - 1;
        }
        else if(target >= nums[0])
        {
            l = 0, r = begin - 1;
        }
        else
        {
            l = begin, r = n - 1;
        }
        while(l <= r)
        {
            int mid = (l + r) >> 1;
            if(nums[mid] > target)
                r = mid - 1;
            else if(nums[mid] < target)
                l = mid + 1;
            else
                return mid;
        }
        return -1;
    }
};